//
//  ConstructionController.m
//  Estate
//
//  Created by heyong on 16/6/22.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "ConstructionController.h"
#import "DefaineCell.h"
#import "B_wuLiaoCell.h"
@interface ConstructionController ()
@property (strong, nonatomic)NSMutableArray *titleForSec1;
@property (strong, nonatomic)FMDatabase *db;
@property (strong, nonatomic)NSMutableArray *dataArr;
@end

@implementation ConstructionController
- (UITableView *)tableView{
    if(_tableView==nil){
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-104)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"DefaineCell" bundle:nil] forCellReuseIdentifier:@"deCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"B_wuLiaoCell" bundle:nil] forCellReuseIdentifier:@"textCell"];
        _tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];

    }
    return _tableView;
}
- (FMDatabase *)db{
    if(_db==nil){
        _db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    }
    return  _db;
}

- (NSMutableArray *)dataArr{
    if(_dataArr==nil){
        _dataArr=[NSMutableArray array];
    }
    return _dataArr;
}

- (NSMutableArray *)titleForSec1{
    if(_titleForSec1==nil){
        _titleForSec1=[NSMutableArray arrayWithObjects:@"单位名称:",@"联系电话:" ,@"单位地址:" ,@"邮编:",@"项目经理:",@"总工:",@"安全总监:",@"质检员",@"安全员",@"实验员",@"工长",nil];
    }
    return _titleForSec1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self initData];
}

-(void)initData{
    NSString *sqlStr=[NSString stringWithFormat:@"SELECT ID,PRO_UNIT_NAME,PRO_UNIT_PHONE,PRO_UNIT_ADDRESS,PRO_UNIT_EMAIL FROM PROJECT_UNIT WHERE PRO_ID='%@' and PRO_UNIT_TYPE='UNIT_02'",PROID];
    [self.db open];
    FMResultSet *result=[self.db executeQuery:sqlStr];
    while ([result next]) {
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic setObject:[result stringForColumn:@"PRO_UNIT_NAME"] forKey:@"name"];
        [dic setObject:[result stringForColumn:@"PRO_UNIT_PHONE"] forKey:@"phone"];
        [dic setObject:[result stringForColumn:@"PRO_UNIT_ADDRESS"] forKey:@"address"];
        [dic setObject:[result stringForColumn:@"PRO_UNIT_EMAIL"] forKey:@"email"];
        [dic setObject:@"" forKey:@"xmjl"];
        [dic setObject:@"" forKey:@"zg"];
        [dic setObject:@"" forKey:@"aqzj"];
        [dic setObject:@"" forKey:@"zjy"];
        [dic setObject:@"" forKey:@"aqy"];
        [dic setObject:@"" forKey:@"syz"];
        [dic setObject:@"" forKey:@"gz"];

        NSString *unitId=[result stringForColumn:@"ID"];
        
        NSString *sqlStr2=[NSString stringWithFormat:@"SELECT USER_ID,USER_TYPE FROM PROJECT_USER WHERE UNIT_ID='%@'",unitId];
        FMResultSet *result2=[self.db executeQuery:sqlStr2];
        while ([result2 next]) {
            //项目经理
            if([[result2 stringForColumn:@"USER_TYPE"]isEqualToString:@"CONSTRACT_01"]){
                
                [dic setObject:[self queryByType:@"CONSTRACT_01" unitId:unitId] forKey:@"xmjl"];
            }
            
            //总工
            if([[result2 stringForColumn:@"USER_TYPE"]isEqualToString:@"CONSTRACT_02"]){
                [dic setObject:[self queryByType:@"CONSTRACT_02" unitId:unitId] forKey:@"zg"];
                
            }
            //安全总监
            if([[result2 stringForColumn:@"USER_TYPE"]isEqualToString:@"CONSTRACT_03"]){
                [dic setObject:[self queryByType:@"CONSTRACT_03" unitId:unitId] forKey:@"aqzj"];

            }
            //质检员
            if([[result2 stringForColumn:@"USER_TYPE"]isEqualToString:@"CONSTRACT_04"]){
                [dic setObject:[self queryByType:@"CONSTRACT_04" unitId:unitId] forKey:@"zjy"];

            }
            //安全员
            if([[result2 stringForColumn:@"USER_TYPE"]isEqualToString:@"CONSTRACT_05"]){
                [dic setObject:[self queryByType:@"CONSTRACT_05" unitId:unitId] forKey:@"aqy"];

            }
            //实验员
            if([[result2 stringForColumn:@"USER_TYPE"]isEqualToString:@"CONSTRACT_06"]){
                [dic setObject:[self queryByType:@"CONSTRACT_06" unitId:unitId] forKey:@"syy"];

            }
            //工长
            if([[result2 stringForColumn:@"USER_TYPE"]isEqualToString:@"CONSTRACT_07"]){
                [dic setObject:[self queryByType:@"CONSTRACT_07" unitId:unitId] forKey:@"gz"];

            }
        }
        [self.dataArr addObject:dic];
    }
    [self.db close];

}

//根据项目角色查询人员

- (NSString *)queryByType:(NSString *)type unitId:(NSString *)unitId{
    NSString *sql=[NSString stringWithFormat:@"SELECT USER_ID FROM PROJECT_USER WHERE USER_TYPE='%@' AND UNIT_ID='%@'",type,unitId];
    FMResultSet *result=[_db executeQuery:sql];
    NSMutableArray *arrName=[NSMutableArray array];
    while ([result next]) {
        NSString *userid=[result stringForColumn:@"USER_ID"];
        [arrName addObject:DEFISNUL([LSUtility getuserIdByUserName:userid][@"REAL_NAME"])];
    }
    NSString *str=@"";
    
    for (NSString *s in arrName) {
        str=[str stringByAppendingString:[NSString stringWithFormat:@"%@\t",s]];
    }
    return str;
}


#pragma mark -UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 0;
    }
    return 20;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 11;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    long row=indexPath.row;
    if(row==8||row==9||row==10||row==7){
        return 120;
    }
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    long row=indexPath.row;
    long section=indexPath.section;
    NSDictionary *dic=self.dataArr[section];
    if(row==0)
    {
        
        DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
        
        [cell setTitle:self.titleForSec1[row] andDetail:dic[@"name"] andLeftConstraint:5];
        
        return cell;
        
    }
    else if(row==1)
    {
        
        DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
        
        [cell setTitle:self.titleForSec1[row] andDetail:dic[@"phone"] andLeftConstraint:5];
        
        return cell;
    }
    else if(row==2)
    {
        
        DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
        
        [cell setTitle:self.titleForSec1[row] andDetail:dic[@"address"] andLeftConstraint:5];
        
        return cell;
    }
    else if(row==3)
    {
        DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
        
        [cell setTitle:self.titleForSec1[row] andDetail:dic[@"email"] andLeftConstraint:5];
        
        return cell;
        
    }
    else if(row==4)
    {
        
        DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
        
        [cell setTitle:self.titleForSec1[row] andDetail:dic[@"xmjl"] andLeftConstraint:5];
        
        return cell;
    }
    else if(row==5)
    {
        
        DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
        
        [cell setTitle:self.titleForSec1[row] andDetail:dic[@"zg"] andLeftConstraint:5];
        
        return cell;
    }
    else if(row==6)
    {
        
        DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
        
        [cell setTitle:self.titleForSec1[row] andDetail:dic[@"aqzj"] andLeftConstraint:5];
        
        return cell;
    }
    else if(row==7)
    {
        
        B_wuLiaoCell *textCell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
        
        textCell.isEdit=NO;

        [textCell setTitle:self.titleForSec1[row] andPlaceHolder:@"" andTextValue:dic[@"zjy"]];
        
        return textCell;
        
    }else if (row==8){
        
        B_wuLiaoCell *textCell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
        
        textCell.isEdit=NO;
        
        [textCell setTitle:self.titleForSec1[row] andPlaceHolder:@"" andTextValue:dic[@"aqy"]];

        
        return textCell;
        
    }else if (row==9){
        B_wuLiaoCell *textCell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
        textCell.isEdit=NO;

        [textCell setTitle:self.titleForSec1[row] andPlaceHolder:@"" andTextValue:dic[@"syy"]];
        return textCell;
    }else{
        B_wuLiaoCell *textCell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
        textCell.isEdit=NO;
        [textCell setTitle:self.titleForSec1[row] andPlaceHolder:@"" andTextValue:dic[@"gz"]];
        return textCell;
    }

    
}


/*
 *  此方法让cell横线置顶
 */
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
